Skip to content

feat(auth): use browser.alarms for JWT refresh (Phase 3)#269

Merged
rosscado merged 1 commit intomainfrom
feat/auth-flow-phase3-token-refresh
Jan 1, 2026
Merged

feat(auth): use browser.alarms for JWT refresh (Phase 3)#269
rosscado merged 1 commit intomainfrom
feat/auth-flow-phase3-token-refresh

Conversation

@rosscado
Copy link
Copy Markdown
Contributor

Summary

Phase 3 of the auth flow revamp: Replace setTimeout with browser.alarms API for scheduling JWT token refresh. This ensures token refresh survives service worker suspension, which is critical for maintaining authentication state in MV3 extensions.

Problem

In Manifest V3 extensions, service workers can be suspended at any time by the browser. When this happens, any scheduled setTimeout callbacks are lost, meaning JWT refresh might never occur, leading to expired tokens and unexpected auth failures.

Solution

Use browser.alarms API which persists across service worker restarts:

  1. Alarm-based scheduling: Token refresh is scheduled using browser.alarms.create() instead of setTimeout
  2. Alarm handler: Background script listens for alarm events and triggers refresh
  3. Graceful fallback: Falls back to setTimeout in contexts where alarms API is unavailable (e.g., content scripts)

Exponential Backoff

When refresh fails, the system retries with exponential backoff:

  • 1st failure: Retry after 1 minute
  • 2nd failure: Retry after 2 minutes
  • 3rd failure: Clear auth state, emit jwt:auth:failed event

This prevents hammering the server during outages while still attempting recovery.

Files Changed

File Changes
wxt.config.ts Add "alarms" permission
src/JwtManager.ts Replace setTimeout with browser.alarms, add exponential backoff
src/svc/background.ts Add alarm handler for JWT refresh

Test plan

  • Build succeeds
  • All 655 tests pass
  • Manual test: Verify token refreshes after service worker suspension
  • Manual test: Verify exponential backoff on refresh failure
  • Manual test: Verify auth clears after 3 failures

🤖 Generated with Claude Code

Replace setTimeout with browser.alarms API for scheduling JWT token
refresh. This ensures token refresh survives service worker suspension.

Key changes:
- Add "alarms" permission to manifest
- Replace setTimeout with browser.alarms.create() in JwtManager
- Add alarm handler in background.ts
- Implement exponential backoff for refresh failures (1min, 2min, 4min)
- Clear auth and emit jwt:auth:failed event after 3 consecutive failures
- Fallback to setTimeout in contexts where alarms API is unavailable

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@rosscado rosscado merged commit 6d0c8da into main Jan 1, 2026
1 check passed
@rosscado rosscado deleted the feat/auth-flow-phase3-token-refresh branch January 1, 2026 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant